package com.sunday.android.fangdaifu.bukong.activity.index;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.InstallEquipment;
import com.sunday.android.fangdaifu.bukong.entity.event.EquipmentList;
import com.sunday.android.fangdaifu.bukong.entity.event.FinishOption;
import com.sunday.android.fangdaifu.bukong.fragment.index.EquipmentFragment;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 设备安装
* */
public class InstallEquipmentActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.lTabItems)
    LinearLayout lTabItems;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind({R.id.tvName01,R.id.tvName02,R.id.tvName03})
    List<TextView> nameList;
    @Bind({R.id.point01,R.id.point02,R.id.point03})
    List<TextView> pointList;
    @Bind({R.id.tvNum01,R.id.tvNum02,R.id.tvNum03})
    List<TextView> numList;
    @Bind({R.id.tab01,R.id.tab02,R.id.tab03})
    List<RelativeLayout> relativeLayoutList;

    private long id;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_equipment);
        ButterKnife.bind(this);
        id = getIntent().getLongExtra("id",0);
        initView();
    }

    private void initView() {
        titleView.setText("设备匹配");
        fragments.add(EquipmentFragment.instance(id,0));
        fragments.add(EquipmentFragment.instance(id,1));
        fragments.add(EquipmentFragment.instance(id,2));
        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(this);
        viewPager.setOffscreenPageLimit(3);
        setSelectItem(0);
    }

    public void onEvent(FinishOption option){
        fragments.clear();
        initView();
    }

    @OnClick({R.id.tab01, R.id.tab02, R.id.tab03})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab01:
                setSelectItem(0);
                break;
            case R.id.tab02:
                setSelectItem(1);
                break;
            case R.id.tab03:
                setSelectItem(2);
                break;
        }
    }

    private void setSelectItem(int position){
        for (int i = 0;i<3;i++){
            nameList.get(i).setSelected(false);
            pointList.get(i).setBackgroundColor(Color.TRANSPARENT);
            nameList.get(i).setTypeface(Typeface.DEFAULT,Typeface.NORMAL);
            if (position==i){
                nameList.get(i).setSelected(true);
                pointList.get(i).setBackgroundColor(Color.parseColor("#000000"));
                nameList.get(i).setTypeface(Typeface.DEFAULT_BOLD,Typeface.BOLD);
                viewPager.setCurrentItem(i,false);
            }
        }
    }

    public void onEvent(EquipmentList equipmentL){
        numList.get(equipmentL.type).setText(String.format("(%d)",equipmentL.size));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setSelectItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class Adapter extends FragmentPagerAdapter{

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
